package cn.six1943.deepchat.module.ai.api;

import cn.six1943.deepchat.module.ai.dal.dataobject.model.ModelDO;
import cn.six1943.deepchat.module.ai.dal.dataobject.model.ProviderInstDO;
import cn.six1943.deepchat.module.ai.enums.ApiConstants;
import cn.six1943.deepchat.module.ai.util.AiHelper;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(ApiConstants.SERVICE_OPENAI)
public class OpenAiModelApi implements IModelApi {

    @Override
    public List<ModelDO> getModels(ProviderInstDO inst) {
        OpenAIClient client = OpenAIOkHttpClient.builder()
                .baseUrl(inst.getBaseUrl())
                .apiKey(inst.getApiAk())
                .build();
        return client.models().list().autoPager().stream().map(one ->AiHelper.openai2Model(one)).toList();
    }

    public static void main(String[] args) {
        ProviderInstDO inst = ProviderInstDO.builder()
                // .baseUrl("https://api.chatanywhere.tech/v1")
                // .apiSk("sk-4aHhLssAxHZWiapoziCALuB7nOVsLLtNeJtQmkjyr6D1RDu1")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .apiSk("sk-ba415ec1d0b34555bdf9eda242701b18")
                .build();
        new OpenAiModelApi().getModels(inst);
    }
}
